package com.zealfi.wk.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zealfi.wk.service.HelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created by kuan at 2018-04-11 上午 10:14
 */
@Service
public class HelloServiceImpl implements HelloService {
    public static final Logger log = LoggerFactory.getLogger(HelloServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Override
    @HystrixCommand(fallbackMethod = "helloFallback", commandKey = "helloKey")
    public String hello() {
        long start = System.currentTimeMillis();
        String result = restTemplate.getForObject("http://HELLO-SERVICE/hello", String.class);
        long end = System.currentTimeMillis();

        log.info("花费时间:{}毫秒", end - start);

        return result;
    }

    @Override
    public String helloFallback() {
        return "error";
    }
}
